<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { equipApi } from '@/api/controller';
  import { ToastSuccess, Toastloading, allNumber } from '@/utils/public';
  const formLabelWidth = '130px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:''
  })
  const options = [
    {value:0,label:'关闭'},
    {value:1,label:'开启'}
  ]
  interface formQuert {
    price:number
    new_protect_num:number
    day_num_1:number
    day_num_2:number
    no_protect_num:number
    if_white:number
    if_screen:number
    if_new_protect:number
    if_day_protect:number
    if_float:number
    float_min:number
    float_max:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    if_screen: [{ required: true, trigger: 'blur', message: '请选择飘屏公告状态' }],
    if_new_protect: [{ required: true, trigger: 'blur', message: '请选择新人保护状态' }],
    new_protect_num: [{ required: true, trigger: 'blur', message: '请输入新用户保护次数' }],
    if_day_protect: [{ required: true, trigger: 'blur', message: '请选择每日保护状态' }],
    day_num_1: [{ required: true, trigger: 'blur', message: '请输入每日保护次数1' }],
    day_num_2: [{ required: true, trigger: 'blur', message: '请输入每日保护次数2' }],
    if_white: [{ required: true, trigger: 'blur', message: '请选择白名单功能状态' }],
    no_protect_num: [{ required: true, trigger: 'blur', message: '请输入大户额度' }],
    price: [{ required: true, trigger: 'blur', message: '请输入单次价格' }],
    if_float: [{ required: true, trigger: 'blur', message: '请选择浮动概率状态' }],
    float_max: [{ required: true, trigger: 'blur', message: '请输入浮动概率利上限' }],
    float_min: [{ required: true, trigger: 'blur', message: '请输入浮动概率利下限' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      equipApi.gameEdit(form).then(res=>{
        loading.close()
        $emit('updateList')
        ToastSuccess('配置修改成功')
        closeDialog()
      }).catch(()=>{
        loading.close()
      })
      
    })
  }
  const openDialog = (query:any = {})=> {
    const {
      game_name='', game_id, if_screen, if_new_protect,new_protect_num,
      if_day_protect, day_num_1, day_num_2, if_white,
      no_protect_num, price, if_float, float_min, float_max
    } = query
    action.title = game_name
    Object.assign(form,{
      game_id, if_screen, if_new_protect,new_protect_num,
      if_day_protect, day_num_1, day_num_2, if_white,
      no_protect_num, price, if_float, float_min, float_max
    })
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="55%"
    :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item :label-width="formLabelWidth" label="飘屏公告状态" prop="if_screen">
              <el-select v-model="form.if_screen" >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="新人保护状态" prop="if_new_protect">
              <el-select v-model="form.if_new_protect">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="新用户保护次数" prop="new_protect_num">
              <el-input v-model="form.new_protect_num" @input="allNumber(form,'new_protect_num')" placeholder="请输入新用户保护次数" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="每日保护状态" prop="if_day_protect">
              <el-select v-model="form.if_day_protect">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="每日保护次数1" prop="day_num_1">
              <el-input v-model="form.day_num_1" @input="allNumber(form,'day_num_1')" placeholder="请输入每日保护次数1" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="每日保护次数2" prop="day_num_2">
              <el-input v-model="form.day_num_2" @input="allNumber(form,'day_num_2')" placeholder="请输入每日保护次数2" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label-width="formLabelWidth" label="白名单功能状态" prop="if_white">
              <el-select v-model="form.if_white">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="大户额度" prop="no_protect_num">
              <el-input v-model="form.no_protect_num" @input="allNumber(form,'no_protect_num')" placeholder="请输入大户额度" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="单次价格" prop="price">
              <el-input v-model="form.price" @input="allNumber(form,'price')" placeholder="请输入单次价格" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="浮动概率状态" prop="if_float">
              <el-select v-model="form.if_float">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="浮动概率利上限" prop="float_max">
              <el-input v-model="form.float_max" @input="allNumber(form,'float_max')" placeholder="请输入浮动概率利上限" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="浮动概率利下限" prop="float_min">
              <el-input v-model="form.float_min" @input="allNumber(form,'float_min')" placeholder="请输入浮动概率利下限" />
            </el-form-item>
          </el-col>
        </el-row>
        
      </el-form>

      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>